/*!
 * \file frdb_get_specified_resultset.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include <frdb_cursor.h>

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __case1(void)
{
    int i_ret = 0;
    Sql_resultset *p_st_resultset = NULL;
    Sql_field_bind *p_st_bind = NULL;
    Sql_statement *p_st_statement = NULL;

    p_st_resultset = frdb_create_resultset(2);
    p_st_resultset->l_id = 1;

    p_st_bind = (Sql_field_bind *)frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x00, sizeof(Sql_field_bind));

    i_ret = frdb_add_resultset_bind(p_st_resultset, p_st_bind);

    p_st_statement = frdb_create_statement();
    i_ret = frdb_add_resultset(p_st_statement, p_st_resultset);

    p_st_resultset = frdb_create_resultset(2);
    p_st_resultset->l_id = 2;

    p_st_bind = (Sql_field_bind *)frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x00, sizeof(Sql_field_bind));

    i_ret = frdb_add_resultset_bind(p_st_resultset, p_st_bind);
    i_ret = frdb_add_resultset(p_st_statement, p_st_resultset);

    p_st_resultset = frdb_get_specified_resultset(p_st_statement, 1);

    CU_ASSERT_EQUAL(p_st_resultset->l_id, 1);

    frdb_statement_free(&p_st_statement);
    return;

}


static void __case2(void)
{
    int i_ret = 0;
    Sql_resultset *p_st_resultset = NULL;
    Sql_field_bind *p_st_bind = NULL;
    Sql_statement *p_st_statement = NULL;

    p_st_resultset = frdb_create_resultset(2);
    p_st_resultset->l_id = 2;

    p_st_bind = (Sql_field_bind *)frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x00, sizeof(Sql_field_bind));

    i_ret = frdb_add_resultset_bind(p_st_resultset, p_st_bind);

    p_st_statement = frdb_create_statement();
    i_ret = frdb_add_resultset(p_st_statement, p_st_resultset);

    p_st_resultset = frdb_create_resultset(2);
    p_st_resultset->l_id = 2;

    p_st_bind = (Sql_field_bind *)frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x00, sizeof(Sql_field_bind));

    i_ret = frdb_add_resultset_bind(p_st_resultset, p_st_bind);
    i_ret = frdb_add_resultset(p_st_statement, p_st_resultset);

    p_st_resultset = frdb_get_specified_resultset(p_st_statement, 2);

    CU_ASSERT_EQUAL(p_st_resultset->l_id, 2);

    frdb_statement_free(&p_st_statement);
    return;

}

static void __case3(void)
{
    int i_ret = 0;
    Sql_resultset *p_st_resultset = NULL;
    Sql_field_bind *p_st_bind = NULL;
    Sql_statement *p_st_statement = NULL;

    p_st_resultset = frdb_create_resultset(2);
    p_st_resultset->l_id = 1;

    p_st_bind = (Sql_field_bind *)frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x00, sizeof(Sql_field_bind));

    i_ret = frdb_add_resultset_bind(p_st_resultset, p_st_bind);

    p_st_statement = frdb_create_statement();
    i_ret = frdb_add_resultset(p_st_statement, p_st_resultset);

    p_st_resultset = frdb_create_resultset(2);
    p_st_resultset->l_id = 2;

    p_st_bind = (Sql_field_bind *)frmalloc(sizeof(Sql_field_bind));
    memset(p_st_bind, 0x00, sizeof(Sql_field_bind));

    i_ret = frdb_add_resultset_bind(p_st_resultset, p_st_bind);
    i_ret = frdb_add_resultset(p_st_statement, p_st_resultset);

    p_st_resultset = frdb_get_specified_resultset(p_st_statement, 3);

    CU_ASSERT_PTR_NULL(p_st_resultset);

    frdb_statement_free(&p_st_statement);
    return;

}


int add_suite_frdb_get_specified_resultset(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frdb_cursor frdb_get_specified_resultset()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);


    return FR_SUCC;
}
